package com.jmu.edu.IMApplication.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.imageview.ShapeableImageView;
import com.jmu.edu.IMApplication.MyApplication;
import com.jmu.edu.IMApplication.R;
import com.jmu.edu.IMApplication.net.bean.Match;
import com.jmu.edu.IMApplication.fragment.ChatFragment;
import com.jmu.edu.IMApplication.utils.ViewUtils;
import com.jmu.edu.IMApplication.utils.WillowUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.UserInfo;

public class MatchItemAdapter extends RecyclerView.Adapter<MatchItemAdapter.MyViewHolder> {

    /**
     * 需要的数据是用户列表、存储用户共同爱好和匹配值的列表
     */
    private List<Match> matchList;
    private Context context;

    public void setMatchList(List<Match> matchList) {
        this.matchList = matchList;
        Collections.sort(matchList, new Comparator<Match>() {
            @Override
            public int compare(Match o1, Match o2) {
                return Double.compare(o2.getMatching(),o1.getMatching());
            }
        });
    }

    public void setContext(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_match, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Match match = matchList.get(position);
        //这个获取账号头像的方法待修改
        WillowUtils.getUserInfo(match.getOtherUserName(), new GetUserInfoCallback() {
            @Override
            public void gotResult(int i, String s, UserInfo userInfo) {
                if (i == 0) {
                    userInfo.getAvatarBitmap(new GetAvatarBitmapCallback() {
                        @Override
                        public void gotResult(int i, String s, Bitmap bitmap) {
                            if (bitmap != null) {
                                holder.avatarImage.setImageBitmap(bitmap);
                            }
                        }
                    });
                }
            }
        });
        holder.usernameText.setText(match.getOtherUserName());
        //传入爱好列表
        holder.tagList.removeAllViews();
        addAllTagsToLayout(holder.tagList, match.getMatchTagList());
        addAllTagsToLayout(holder.tagList, match.getMatchHobbiesList());
        //这里的匹配度也是来自还没传进来的列表的列表
        holder.degreeText.setText("匹配度:" + String.format("%.0f", match.getMatching() * 100) + "%");
        //点击用户跳转到对话界面
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tag = "chat";
                ChatFragment chatFragment = (ChatFragment) MyApplication.fragmentManager.findFragmentByTag(tag);
                if (chatFragment == null) {
                    chatFragment = new ChatFragment();
                }
                //传递必须的参数
                Bundle bundle = new Bundle();
                bundle.putString("username", match.getOtherUserName());
                bundle.putString("friendName", "陌生人");
                bundle.putString("state", "在线");
                chatFragment.setArguments(bundle);
                ViewUtils.showFragment(chatFragment, tag, R.anim.from_right, R.anim.out_right);
            }
        });

    }

    @Override
    public int getItemCount() {
        return matchList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private ShapeableImageView avatarImage;
        private TextView usernameText, degreeText;
        private LinearLayout tagList;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            avatarImage = itemView.findViewById(R.id.item_match_avatar);
            usernameText = itemView.findViewById(R.id.item_match_username);
            degreeText = itemView.findViewById(R.id.item_match_degree);
            tagList = itemView.findViewById(R.id.item_match_tagList);
        }
    }

    private void addAllTagsToLayout(LinearLayout linearLayout, ArrayList<String> tags) {
        for (String tag : tags
        ) {
            View view = View.inflate(context, R.layout.item_tag_small, null);
            TextView textView = view.findViewById(R.id.item_tag_name);
            textView.setText(tag);
            linearLayout.addView(view);
            Log.d("匹配信息适配器.动态生成标签", "addAllTagsToLayout: " + tag);
        }

    }

}
